package com.gkd.controller;


import com.gkd.common.QueryPageParam;
import com.gkd.common.Result;
import com.gkd.mapper.UserMapper;
import com.gkd.pojo.User;
import com.gkd.pojo.UserPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;

    //分页列表
    @PostMapping("/listPage")
    public Result userList(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        int pageNum = query.getPageNum() - 1;
        int pageSize = query.getPageSize();
        int roleId = (int)param.get("roleId");


        List<User> pageUser = userMapper.getPageUser( roleId,pageNum * pageSize, pageSize);
        if (pageUser != null) {
            return Result.result(200, "获取数据成功", userMapper.getAllUser(roleId).size(), pageUser);
        }
        return Result.fail();
    }

    //新增数据
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        User isUser = userMapper.getByIdAndPwdUser(user.getNo(), user.getPassword());
        if (isUser != null) {
            //如果用户已经存在
            return Result.addFail(userMapper.getAllUser(user.getRoleId()).size(), "用户已存在");
        }

        int i = userMapper.addUser(user);
        if (i > 0) {
            return Result.addSuc(userMapper.getAllUser(user.getRoleId()).size());
        }
        return Result.addFail(userMapper.getAllUser(user.getRoleId()).size(), "新增数据失败");
    }

    //删除数据
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody User user) {
        int id = user.getId();
        int i = userMapper.deleteUser(id);
        if (i > 0) {
            return Result.deleteSuc(userMapper.getAllUser(user.getRoleId()).size());
        }
        return Result.deleteFail(userMapper.getAllUser(user.getRoleId()).size());
    }

    //修改数据
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {

        User isUser = userMapper.getByIdAndPwdUser(user.getNo(), user.getPassword());
        if (isUser != null) {
            //如果用户已经存在
            return Result.updateFail(userMapper.getAllUser(user.getRoleId()).size(), "用户已存在");
        }
        int i = userMapper.updateUser(user);
        if (i > 0) {
            return Result.updateSuc(userMapper.getAllUser(user.getRoleId()).size());
        }
        return Result.updateFail(userMapper.getAllUser(user.getRoleId()).size(), "修改数据失败");
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String no = user.getNo();
        String password = user.getPassword();
        user = userMapper.getByIdAndPwdUser(no, password);
        if (user != null) {
            return Result.suc(user);
        }
        return Result.err();
    }

    //注册页面
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        User isUser = userMapper.getByIdAndPwdUser(user.getNo(), user.getPassword());
        if (isUser != null) {
            //如果用户已经存在
            return Result.addFail(userMapper.getAllUser(user.getRoleId()).size(), "用户已存在");
        }

        int i = userMapper.addUser(user);
        if (i > 0) {
            return Result.registerSuc(userMapper.getAllUser(user.getRoleId()).size());
        }
        return Result.registerFail(userMapper.getAllUser(user.getRoleId()).size(), "注册用户失败");
    }

    @PostMapping("/getUser")
    public Result getUser(@RequestBody User user) {
        int id = user.getId();
        user = userMapper.getByIdUser(id);
        if (user != null) {
            return Result.suc(userMapper.getAllUser(user.getRoleId()).size(), user);
        }
        return Result.fail();
    }

    //模糊查询
    @PostMapping("/likeUser")
    public Result likeUser(@RequestBody UserPage userPage) {
        int pageNum = userPage.getPageNum() - 1;
        int pageSize = userPage.getPageSize();
        int roleId = userPage.getRoleId();

        String sex = "";
        if (!userPage.getSex().equals("")) {
            sex = userPage.getSex().equals("男") ? "1" : "0";
        }
        String name = userPage.getName();
        if (!sex.equals("") && !name.equals("")) {
            List<UserPage> users = userMapper.getBySexAndNamePageUser(roleId, name, sex, pageNum * pageSize, pageSize);
            return Result.result(200, "查询数据成功", userMapper.getBySexAndNameUser(roleId, name, sex).size(), users);
        }
        if (!sex.equals("")) {
            List<UserPage> users = userMapper.getBySexPageUser(roleId, sex, pageNum * pageSize, pageSize);
            return Result.result(200, "查询数据成功", userMapper.getBySexUser(roleId, sex).size(), users);
        }
        if (!name.equals("")) {
            List<UserPage> users = userMapper.getByNamePageUser(roleId, name, pageNum * pageSize, pageSize);
            return Result.result(200, "查询数据成功", userMapper.getByNameUser(roleId, name).size(), users);
        }
        return Result.fail();
    }
}
